package com.sanjiahantang.youjiaclient.module.me.contract.views.impl;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.module.me.contract.preseneters.ContractPresenter;
import com.sanjiahantang.youjiaclient.module.me.contract.views.ContractView;

import static com.sanjiahantang.youjiaclient.config.StaticValue.INTENT_CONTRACT;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_ID;


/**
 * Created by zy on 2017/9/8.
 */

public class ContractActivity extends BaseMvpActivity<ContractView, ContractPresenter>
        implements ContractView, View.OnClickListener {
    private RadioButton rbAll, rbUnSign, rbSigned;
    private ListView listView;

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public ContractPresenter initPresenter() {
        return new ContractPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_me_contract);
        rbAll = (RadioButton) findViewById(R.id.rbAll);
        rbUnSign = (RadioButton) findViewById(R.id.rbUnSign);
        rbSigned = (RadioButton) findViewById(R.id.rbSigned);
        listView = (ListView) findViewById(R.id.listView);
        findViewById(R.id.flAll).setOnClickListener(this);
        findViewById(R.id.flUnSign).setOnClickListener(this);
        findViewById(R.id.flSigned).setOnClickListener(this);
        listView.setAdapter(presenter.initAdapter(this));
        rbAll.setOnClickListener(this);
        rbUnSign.setOnClickListener(this);
        rbSigned.setOnClickListener(this);

        TextView title = (TextView) findViewById(R.id.title);
        title.setText("我的合同");
        findViewById(R.id.fl_Left).setOnClickListener(this);
    }

    @Override
    public void initData() {
        rbAll.setChecked(true);
        rbUnSign.setChecked(false);
        rbSigned.setChecked(false);
        presenter.loadData($this, TYPE_ALL);
    }

    /**
     * 由于页面效果限制，不使用radioGroup的onChange
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.flAll:
            case R.id.rbAll:
                rbAll.setChecked(true);
                rbUnSign.setChecked(false);
                rbSigned.setChecked(false);
                presenter.loadData($this, TYPE_ALL);
                break;
            case R.id.flUnSign:
            case R.id.rbUnSign:
                rbAll.setChecked(false);
                rbUnSign.setChecked(true);
                rbSigned.setChecked(false);
                presenter.loadData($this, TYPE_UNSIGNED);
                break;
            case R.id.flSigned:
            case R.id.rbSigned:
                rbAll.setChecked(false);
                rbUnSign.setChecked(false);
                rbSigned.setChecked(true);
                presenter.loadData($this, TYPE_SIGNED);
                break;
            case R.id.fl_Left:
                finish();
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null || data.getExtras() == null) {
            rbAll.setChecked(true);
            rbUnSign.setChecked(false);
            rbSigned.setChecked(false);
            presenter.loadData($this, TYPE_ALL);
            return;
        }
        if (requestCode == 3 || requestCode == 4) {
            Bundle bundle = data.getExtras();
            String type = bundle.getString(ContractView.KEY_CONTRACT_TYPE);
            if (type.equals("0")) {
                rbAll.setChecked(false);
                rbUnSign.setChecked(true);
                rbSigned.setChecked(false);
                presenter.loadData($this, TYPE_UNSIGNED);
            } else if (type.equals("1")) {
                rbAll.setChecked(false);
                rbUnSign.setChecked(false);
                rbSigned.setChecked(true);
                presenter.loadData($this, TYPE_SIGNED);
            } else {
                rbAll.setChecked(true);
                rbUnSign.setChecked(false);
                rbSigned.setChecked(false);
                presenter.loadData($this, TYPE_ALL);
            }
        }
    }

}
